package cn.v2tila.rsosa.management.controller;

import cn.v2tila.rsosa.management.entity.DeliveryEntity;
import cn.v2tila.rsosa.management.entity.VitaeEntity;
import cn.v2tila.rsosa.management.entity.VitaeImgsEntity;
import cn.v2tila.rsosa.management.entity.ZpxxEntity;
import cn.v2tila.rsosa.management.service.DeliveryService;
import cn.v2tila.rsosa.management.service.VitaeImgsService;
import cn.v2tila.rsosa.management.service.VitaeService;
import cn.v2tila.rsosa.management.service.ZpxxService;
import cn.v2tila.rsosa.management.utils.PageDataResult;
import cn.v2tila.rsosa.management.vo.ResultVO;
import cn.v2tila.rsosa.management.vo.changeCodeVO;
import net.sf.oval.guard.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author v2tila on 16:50 2019/4/18
 */
@Controller
@RequestMapping(value = "zpxx")
public class zpxxController {

    @Autowired
    private ZpxxService zpxxService;
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private VitaeService vitaeService;
    @Autowired
    private VitaeImgsService vitaeImgsService;

    @RequestMapping(value = "zpxxInfo")
    public String toZpxxInfo() {
        return "zpxx/zpxxInfo";
    }

    @RequestMapping(value = "deliveryInfo")
    public String toDeliveryInfo() {
        return "zpxx/deliveryInfo";
    }

    @RequestMapping(value = "vitaeInfo")
    public String toVitaeInfo() {
        return "zpxx/vitaeInfo";
    }

    @GetMapping(value = "getZpxx")
    @ResponseBody
    public PageDataResult getUsers(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   ZpxxEntity zpxxEntity){
        if(zpxxEntity.getAssoName().equals("")){
            zpxxEntity.setAssoName(null);
        }
        if(zpxxEntity.getSchoolName().equals("")){
            zpxxEntity.setSchoolName(null);
        }
        if(zpxxEntity.getCollegeName().equals("")){
            zpxxEntity.setCollegeName(null);
        }

        return zpxxService.getZpxx(page, limit,zpxxEntity);
    }

    @GetMapping(value = "getDelivery")
    @ResponseBody
    public PageDataResult getDelivery(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   DeliveryEntity deliveryEntity){
        if(deliveryEntity.getUniqueId().equals("")){
            deliveryEntity.setUniqueId(null);
        }
        return deliveryService.getDeliver(page, limit,deliveryEntity);
    }

    @GetMapping(value = "getVitae")
    @ResponseBody
    public PageDataResult getVitae(@RequestParam("page") Integer page,
                                      @RequestParam("limit") Integer limit,
                                      VitaeEntity vitaeEntity){
        if(vitaeEntity.getUniqueId().equals("")){
            vitaeEntity.setUniqueId(null);
        }
        return vitaeService.getVitae(page, limit,vitaeEntity);
    }
    @GetMapping(value = "getVitaeImgs")
    @ResponseBody
    public PageDataResult getVitae(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   VitaeImgsEntity vitaeImgsEntity){

        return vitaeImgsService.getVitaeImgs(page, limit,vitaeImgsEntity);
    }

    @PostMapping(value = "changeCode")
    @ResponseBody
    public ResultVO changeCode(@RequestBody changeCodeVO changeCodeVO) {
        ResultVO resultVO = new ResultVO();
        ZpxxEntity zpxxEntity = zpxxService.findById(changeCodeVO.getId());
        if (zpxxEntity == null) {
            resultVO.setMsg("没有该招聘信息");
            resultVO.setCode(0);
        }else {
            zpxxEntity.setCode(changeCodeVO.getCode());
            ZpxxEntity saveIsSuccess = zpxxService.saveAndFlush(zpxxEntity);
            if(saveIsSuccess==null){
                resultVO.setMsg("修改状态失败");
                resultVO.setCode(0);
            }else{
                resultVO.setCode(1);
            }
        }
        return resultVO;
    }

    @PostMapping(value = "delZpxx")
    @ResponseBody
    public ResultVO delZpxx(@RequestBody @RequestParam(value = "id") Integer id){
        ResultVO resultVO = new ResultVO();
        try {
            zpxxService.deleteById(id);
            resultVO.setCode(1);
        } catch (Exception e) {
            resultVO.setCode(0);
            resultVO.setMsg("删除失败");
        }

        return resultVO;
    }
}
